﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pahan.McuFusion.DebugTools.Logging
{
    public struct SignalEvent
    {
        public SignalEvent(String name, bool signal)
        {
            this.signalName = name;
            this.signal = signal;
            this.initialized = true;
        }

        public override string ToString()
        {
            return initialized ?
                String.Format("{0}:{1}", signalName, signal ? 1 : 0) :
                "<Unknown>:<Unknown>";
        }

        public static SignalEvent Parse(String str)
        {
            try
            {
                var parts = str.Split(':');
                bool signal;
                switch (parts[1])
                {
                    case "0":
                        signal = false;
                        break;
                    case "1":
                        signal = true;
                        break;
                    case "<Unknown>":
                        return new SignalEvent();
                    default:
                        throw new FormatException();
                }
                return new SignalEvent(parts[0], signal);
            }
            catch (FormatException)
            {
                throw;
            }
            catch
            {
                throw new FormatException();
            }
        }
        
        public bool Signal
        {
            get { return signal; }
        }

        public String Name
        {
            get { return signalName; }
        }

        public bool Empty
        {
            get { return !initialized; }
        }

        private readonly bool signal;

        private readonly String signalName;

        private readonly bool initialized;
    }
}